package com.kaibes.core.base.util;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.text.DecimalFormat;
import java.util.Random;
import java.util.regex.Pattern;

public class MathUtils {
    
    public static boolean nullOrNotEquals(Number source, Number target) {
        return source == null || !source.equals(target);
    }
    
    public static boolean in(Number source, Number...numbers) {
        for (Number number : numbers) {
            if (source.equals(number)) {
                return true;
            }
        }
        return false;
    }
    
    public static boolean between(Number source, Number start, Number end) {
        return source.longValue() > start.longValue() && source.longValue() < end.longValue();
    }
    
    public static boolean notBetween(Number source, Number start, Number end) {
        return source.longValue() < start.longValue() || source.longValue() > end.longValue();
    }

    public static boolean isNotInteger(String oldValue, String newValue) {
        return newValue.length() - oldValue.length() > 0 && !MathUtils.isInteger(newValue);
    }
    public static boolean isInteger(String str) {
        return str.isEmpty() ? false : Pattern.compile("-?[0-9]+").matcher(str).matches();
    }
    
    public static boolean isNumericOrEmpty(String str) {
        return str.isEmpty() ? true : Pattern.compile("-?[0-9]*\\.?[0-9]{0,2}").matcher(str).matches();
    }
    
    public static boolean isNumeric(String str) {
        return str.isEmpty() ? false : Pattern.compile("-?[0-9]*\\.?[0-9]{0,2}").matcher(str).matches();
    }

    public static String roundMath(float dividend, float divisor){
        String result = "";
        float actionResult = dividend/divisor;
        BigDecimal b = new BigDecimal(actionResult);
        result = String.valueOf(b.setScale(1, RoundingMode.HALF_UP).floatValue());
        return result;
    }

    public static float decimalMath(String format, float source){
        DecimalFormat decimalFormat = new DecimalFormat(format);
        source = Float.parseFloat(decimalFormat.format(source));
        return source;
    }

    public static boolean isLast2StrZero(String sourceNum){
        boolean result = false;
        String last2Str = sourceNum.substring(sourceNum.length() - 2, sourceNum.length());
        if(last2Str.equals(".0")){
            result = true;
        }
        return result;
    }

    public static String convertLast2StrZero2String(String sourceNum){
        String resultStr = "";
        if(isLast2StrZero(sourceNum)){
            resultStr = sourceNum.replace(".0", "");
        }else{
            resultStr = sourceNum;
        }
        return resultStr;
    }

    public static String convertLast2StrZero2String(float sourceNum){
        String resultStr = "";
        String sourceNumStr = String.valueOf(sourceNum);
        if(isLast2StrZero(sourceNumStr)){
            resultStr = sourceNumStr.replace(".0", "");
        }else{
            resultStr = String.valueOf(sourceNum);
        }
        return resultStr;
    }

    public static int toUpNumber(float sourceNum){
       return (int) Math.ceil(sourceNum);
    }

    public static int toDownNumber(float sourceNum){
        return (int) Math.floor(sourceNum);
    }

    public static int toRoundNumber(float sourceNum){
        return Math.round(sourceNum);
    }

    /**
     *
     * @param x
     * @param num
     */
    public static double xround(float x, int num){
        return Math.round(x * Math.pow(10, num)) / Math.pow(10, num) ;
    }

    public static String randomNum(int length) {
        Random random = new Random();
        StringBuilder sb = new StringBuilder();
        for (int i=0; i<length; i++) {
            sb.append(String.valueOf(random.nextInt(10)));
        }
        return sb.toString();
    }
    
    public static int parseIntForce(String value) {
        if (isInteger(value.trim())) {
            return Integer.parseInt(value);
        }
        return 0;
    }
    
}
